﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QSerialPort>
#include <QSerialPortInfo>

//#include <windows.h>
//#include <shellapi.h>

//#include <QDebug>

#include <QAbstractButton>

#include <QWidget>
#include <QMouseEvent>
#include <QFile>
#include <QFileDialog>
#include <QThread>
#include <QTimer>
#include <QDateTime>
#include <QMargins>
#include <QSettings>
#include <QProcess>
#include <QTextEdit>
#include <QLabel>
#include <QVBoxLayout>
#include <QMap>
#include <QSql>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlRecord>

#include "quihelper.h"
#include "iconhelper.h"
#include "qhexmonitor.h"
#include "contactus.h"
#include "comthread.h"
#include "comthread_f210.h"
#include "my_crc.h"


//#define __SYNC_TIMER_DEBUG
#define __UPRG_FPGA
//#define __IMPORT_FILE_DEBUG

const QString AppVers = "1.6.19";    // 重大更新更改第二版本,十位进一
#define UPG_FILE_VERS_F210 "v2.2.1" // FPGA文件发送更改后更改版本

// F210-原彩修复
#define SEND_DATA_SYNC 1    //同步
#define SEND_DATA_REPT 2    //重复
#define SEND_READ_REQ  3    //读请求
#define SEND_BURN_REQ  4    //写请求
#define SEND_BURN_DATA 5    //写数据
#define SEND_BURN_DONE 6    //写完成
#define SEND_DATA_UPG  7    //升级MCU
#define SEND_BURN_MTSN_REQ  8    //写盖板码请求
#define SEND_READ_MTSN_REQ  9    //读盖板码请求
#define SEND_FPGA_UPG  10   //升级FPGA
#define SEND_BURN_MTSN_DEV  11    //写盖板码请求,替换底层
#define SEND_DATA_DONE 12   //升级MCU完成
#define SEND_FPGA_DONE 13   //升级FPGA完成
#define SEND_FILE_SIZE 14   //发送升级文件总大小
#define SEND_PORT_DISC 15   //端口断开
// F210-电池修复
#define SEND_READ_DATA_BAT  16    //读数据
#define SEND_BURN_DATA_BAT  17    //写数据
// F210-显示修复
#define SEND_READ_DISP  18    //读数据
#define SEND_BURN_DISP  19    //写数据
// F316-电池循环
#define SEND_READ_DATA_F316  20    //读数据
#define SEND_BURN_DATA_F316  21    //写数据

#define PORT_STU_NULL 0    		//默认
#define PORT_STU_SYNC 1    		//同步
#define PORT_STU_REPT 2    		//重复
#define PORT_STU_READ 3    		//读
#define PORT_STU_BURN 4    		//写
#define PORT_STU_FREE 5    		//同步完成,空闲状态
#define PORT_STU_UPG  6    		//升级
#define PORT_STU_WAIT 7    		//待同步
#define PORT_STU_RDOK 8    		//读完成
#define PORT_STU_READ_MTSN 9    //读盖板码
#define PORT_STU_BURN_MTSN 10   //写盖板码
#define PORT_STU_TIMEOUT   11   //超时
#define PORT_STU_BUSY      12   //忙
#define PORT_STU_ERROR     13   //错误
#define PORT_STU_WAIT_OK   14   //等待设备完成

#define DISP_BTN_LOCK_ALL 0
#define DISP_BTN_UNLOCK_ALL 1
#define DISP_BTN_LOCK_RES 2

#define MSG_STU_ERROR 0    //错误
#define MSG_STU_MSG   1    //正常
#define MSG_STU_CONT  2    //内容，另一行首行缩进

#define CHECK_FILE_NAME 0
#define CHECK_FILE_PATH 1

#define UPG_FILE_NAME_APP3   "QT5Gui_Ver.dll"   //3位版本号的mcu升级文件
//#define UPG_FILE_NAME_APP2   "QT5Gui_App.dll"   //2位版本号的mcu升级文件
#define UPG_FILE_NAME_FPGA   "QT5Gui_Dev.dll"

#define UPG_FILE_APP_F316   "QT5Gui_Cd.dll"
#define UPG_FILE_APP_F312   "QT5Gui_Ce.dll"

#define UPG_FILE_APP_DL8000   "QT5Gui_DB.dll"
#define UPG_FILE_APP_DL6000   "QT5Gui_DC.dll"

#define ROM_FILE_NAME_I8     "dev/rom_dev_8.bin"
#define ROM_FILE_NAME_I8P    "dev/rom_dev_8p_c3f.bin"
#define ROM_FILE_NAME_I8P_A  "dev/rom_dev_8p_c11.bin"
#define ROM_FILE_NAME_IX     "dev/rom_dev_x.bin"
#define ROM_FILE_NAME_IXR    "dev/rom_dev_xr.bin"
#define ROM_FILE_NAME_IXSMAX "dev/rom_dev_xsmax.bin"
#define ROM_FILE_NAME_I11    "dev/rom_dev_11.bin"
#define ROM_FILE_NAME_I11P   "dev/rom_dev_11p.bin"
#define ROM_FILE_NAME_I12    "dev/rom_dev_12.bin"
#define ROM_FILE_NAME_I12PM  "dev/rom_dev_12pm.bin"
#define ROM_FILE_NAME_I13    "dev/rom_dev_13.bin"
#define ROM_FILE_NAME_I14    "dev/rom_dev_14.bin"
#define ROM_FILE_NAME_I15    "dev/rom_dev_15.bin"
#define ROM_FILE_NAME_I13Pro "dev/rom_dev_13p.bin"
#define ROM_FILE_NAME_I14Pro "dev/rom_dev_14p.bin"

#define TIMER_RECV_TIME_OUT 20
#define TIMER_SYNC_TIME_OUT 3000
#define TIMER_LINK_TIME_OUT 1000
#define TIME_MCU_TIME_OUT   500

#define MODEL_NUM 6

#define MTSN_CODE_ADDR_i8g      0x3A37	// Detect Addr
#define MTSN_CODE_ADDR_iXs      0x120C	// Detect Addr
#define MTSN_CODE_ADDR_1_iXs	  0x236C
#define MTSN_CODE_ADDR_2_iXs	  0x3D2C
#define MTSN_CODE_ADDR_iXr      0x3F00	// Detect Addr
#define MTSN_CODE_ADDR_i11      0x1540	// Detect Addr
#define MTSN_CODE_ADDR_1_i11	  0x14C5
#define MTSN_CODE_ADDR_i11P     0x15A7	// Detect Addr
#define MTSN_CODE_ADDR_1_i11P	  0x1540
#define MTSN_CODE_ADDR_2_i11P	  0x1480
#define MTSN_CODE_ADDR_i12      0x3DA7	// Detect Addr
#define MTSN_CODE_ADDR_1_i12	  0x1480
#define MTSN_CODE_ADDR_2_i12	  0x1540
#define MTSN_CODE_ADDR_i12PM	  0x3DA7	// Detect Addr
#define MTSN_CODE_ADDR_i13      0x00A7	// Detect Addr

#define DLBOX_FONT_SIZE 9
#define NORMAL_FONT_SIZE 9

#define APP_FONT_COLOR_WARNNING QColor("yellow")
#define APP_FONT_COLOR_NORMAL   QColor(0, 222, 255)
#define APP_FONT_COLOR_SUCCESS  QColor(0, 255, 0)
#define APP_FONT_COLOR_FAILED   QColor("red")

const QString Months[] = {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
};

enum{
    Model_NULL = 0,
    Model_X = 1,
    Model_XR,
    Model_XSMax,
    Model_8,
    Model_8P,
    Model_8P_a,
    Model_11,
    Model_11P,
    Model_12,
    Model_12PM,
    Model_13,
    Model_14,
    Model_13P,
    Model_14P,
    Model_15P,
    Model_15,
    Model_16,
    Model_16P,
}Models;

enum{
    Type_Te_Prin = 0,
    Type_te_mtsn,
    Type_Le_Write,
    Type_Pte_Recv_Dlbox,
    Type_Te_Mtsn_Dlbox,
    Type_Le_Write_Dlbox,
    Type_Te_Disp_F210,
    Type_Te_Bat_F210,
    Type_Te_Prin_F316,
	Type_te_mtsn_dl8000,
}OBjectType;

typedef enum{
    BAT_KIND_5G = 0,
    BAT_KIND_5C,
    BAT_KIND_5S,
    BAT_KIND_5SE,
    BAT_KIND_SE,
    BAT_KIND_SE2,
    BAT_KIND_SE3,
    BAT_KIND_6G,
    BAT_KIND_6Plus,
    BAT_KIND_6S,
    BAT_KIND_6SPlus,
    BAT_KIND_7G,
    BAT_KIND_7Plus,
    BAT_KIND_8G,
    BAT_KIND_8Plus,
    BAT_KIND_X,
    BAT_KIND_XS,
    BAT_KIND_XSM,
    BAT_KIND_XR,
    BAT_KIND_11G,
    BAT_KIND_11Pro,
    BAT_KIND_11PM,
    BAT_KIND_12G,
    BAT_KIND_12Pro,
    BAT_KIND_12PM,
    BAT_KIND_12MN,
    BAT_KIND_13G,
    BAT_KIND_13Pro,
    BAT_KIND_13PM,
    BAT_KIND_13MN,
    BAT_KIND_14G,
    BAT_KIND_14Plus,
    BAT_KIND_14Pro,
    BAT_KIND_14PM,
    BAT_KIND_Unknown,
    BAT_KIND_NULL,
}BAT_KIND_ENUM;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class QSqlTableModel;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

// Init
    void Left_Widgt_Init();
    void Config_Param_Init();
    void Param_Init();
    void Gui_Init();
    void Title_Widgt_Init();
    void LeftWidget_TitleBtn_Init();
    void Menu_Init();

    QByteArray Get_MtSN_From_Dev_File();

// 
    QString ByteArray2Hex(QByteArray data);
    QString ByteArray2QString(QByteArray data,int size);
    QByteArray Hex2ByteArray(QString HexString);

// LeftWidget
    void LeftWidget_TitleBtn_Set(QPushButton* btn_title, QString TitleName);
    void LeftWidget_BtnTitle_SetIcon_Move(QPushButton* btn_title, bool* HoverStu);
    void LeftWidget_BtnTitle_SetIcon_Leave(QPushButton* btn_title, bool* HoverStu);
    void LeftWidget_BtnTitle_SetIcon_Release(QPushButton* btn_title, QList<QAbstractButton *> btn_group, bool* ReleaseStu);

    void Left_Layout_Btn_Init(QButtonGroup *btnGroup, bool textBesideIcon);
    void Left_Layout_Btn_Click();


// F210-通信
    void Port_F210_Sync_Timer();			// 同步
    void Check_Port_Status_Each_Time();		// 超时
    void Port_F210_Once_Timer();	// 单次操作定时器
    void Port_F210_Port_Init();
    void Port_Serial_Read_clicked();		// 接收槽函数
    void Port_Serial_Read_Data();			// 接收数据
    void Port_Serial_Cmd_Repeat();			// 重发
    void pc_to_mcu_send_data(QByteArray LineStr,int SendStu);	// 发送包
    bool Wait_TimeOut_to_Send_Three_Time(QByteArray SendBuff, int LastStu, int SendStu);	// 发送包并等待
    bool Wait_TimeOut_to_Wait_Mcu_Answer();					// 长时间等待设备
    void Wait_Time_to_Wait_Mcu(int TimeVal);				// 等待N秒
    void Get_Check_Version_From_File();						// 获取文件中的设备版本
    void Port_Function_Lock_Or_Unlock(int EnabledStu);		// 锁定/解锁按钮
// F210-原彩修复
    bool Upgrade_File_to_Mcu();				// 升级
    bool Mtsn_Write_to_Mcu(int SendCmd);	// 写盖板码
    bool Read_Data_Form_Mcu();				// 读数据
    bool Burn_Data_to_Mcu();				// 写数据
// F210-电池修复
    bool Read_Bat_Data_Form_F210();			// 读数据
    bool Write_Bat_Data_to_F210();			// 读数据
// dl8000
    void Port_dl8000_Sync_Timer();			// 同步
    void Port_dl8000_Once_Timer();			// 同步
    void Port_dl8000_Port_Init();
    bool Port_dl8000_Write_Mtsn(int SendCmd);
    bool Port_dl8000_Upgrade_File();
    void Port_dl8000_Send_Data(QByteArray LineStr,int SendStu);
    void Port_dl8000_Serial_Cmd_Repeat();
    void Port_dl8000_Serial_Read_clicked();
    void Port_dl8000_Serial_Read_Data();
    void Port_dl8000_Function_Lock_Or_Unlock(int EnabledStu);
    bool Port_dl8000_Wait_Answer();					//
    void Port_dl8000_Wait_Stu(int WaitStu);
    bool Port_dl8000_Send_3Time(QByteArray SendBuff, int LastStu, int SendStu);
    void Port_dl8000_Wait_Time(int TimeVal);
	void Lbl_Ver_dl8000_Langure_to_Change();

// Langure
    void App_Langure_to_Change();
    void Btn_Link_Langure_to_Change(QPushButton* btn_link);
    void Lbl_Ver_Langure_to_Change();
    void F210_Langure_Change_Init();
    void F210_Code_Langure_Change_Init();
    void TextBox_Langure_Change_Init();
    void b316_BatLoop_Langure_Change_Init();
    void dl8000_Langure_Change_Init();
    void LangureStr_Init_Btn_Link(QPushButton* btn_link);
    void LangureStr_Init_Btn_Unlink(QPushButton* btn_unlink);
    void LangureStr_Init_Unlink();
    void LangureStr_Init_Testing(QTextEdit *te_prin);
    void LangureStr_Init_LinkFailed(QTextEdit *te_prin);
    void LangureStr_Init_Connected(QTextEdit *te_prin);
    void LangureStr_Init_Disconnected();
    void LangureStr_Init_NotConnected(QTextEdit *te_prin);
    void LangureStr_Init_TimeOut(QTextEdit *te_prin);
    void LangureStr_Init_Busy(QTextEdit *te_prin);
    void LangureStr_Init_FileOpenFail(QTextEdit* te_prin);
    void LangureStr_Init_FileNonExistent(QTextEdit *te_prin);
    void LangureStr_Init_LimitFileSize(QTextEdit *te_prin);
    void LangureStr_Init_BurningCode(QTextEdit *te_prin);
    void LangureStr_Init_BurningCode_DevData(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_Succeed(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_DevData_Succeed(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_Failed(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_DevData_Failed(QTextEdit *te_prin);
    void LangureStr_Init_ReadingData(QTextEdit *te_prin);
    void LangureStr_Init_ReadData_Succeed(QTextEdit *te_prin);
    void LangureStr_Init_ReadData_Failed(QTextEdit *te_prin);
    void LangureStr_Init_BurningData(QTextEdit *te_prin);
    void LangureStr_Init_BurnData_Succeed(QTextEdit *te_prin);
    void LangureStr_Init_BurnData_Failed(QTextEdit *te_prin);
    void LangureStr_Init_BurnData_WaitDev(QTextEdit *te_prin);
    void LangureStr_Init_DevVers_Low(QTextEdit *te_prin, QByteArray BinVersion);
    void LangureStr_Init_AppVers_Low(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_Failed_Busy(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_DevData_Failed_Busy(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_Failed_Time(QTextEdit *te_prin);
    void LangureStr_Init_BurnCode_DevData_Failed_Time(QTextEdit *te_prin);
    void Btn_Link_Langure_to_Set(QPushButton* btn_link);
//
    void InsertText(QTextEdit* te_prin, QString text, int fontSize, QColor fontColor);
    void App_ChooseFile_SavePath(QTextEdit* te_file, QString CfgDir);
    QString App_Btn_ChooseFile_GetPath(QString CfgDir);
    QString App_Btn_ChoosePath_GetPath(QString CfgDir);
    bool App_Check_Legal_MtsnCode(QByteArray MtsnStr);
    void App_Te_Mtsn_Limit_InputLen(QTextEdit* te_mtsn);

    QStringList Check_new_device_detected(QStringList portStringList,QStringList PortList_Link,QString contains_str,int ConnectStu,QTextEdit* te_prin);
    void Check_null_device_detected(QStringList portStringList,QString contains_str,QTextEdit* te_prin);
    bool Check_current_port(QString CurrentPort, int ConnectStu, QComboBox* cbx_port);

    void Body_Load_Style(const QString &qssFile);
    void Title_Load_Style(const QString &qssFile);

    bool App_Check_Legal_FilePath(QString FileName, int CheckStu);
    void App_Write_FilePath_CfgFile(QString CfgVal,QString FileName);
    void CBX_Port_SizeHint_Set(QComboBox* cbx_port);
    QByteArray Get_Version_From_File(QString VersName, QTextEdit *TePrin, uint32_t OffAddr);
	
// dlbox
    void Port_Dlbox_Port_Init();
    // UI
    void Btn_Lock_Or_Unlock_Disp_F210(int EnabledStu);
    // Port
    bool InitSerial_Dlbox(QSerialPort *PortSerial, QString portname);
    bool Wait_Time_to_Recv(QByteArray CmdPage);
    void Port_Dlbox_Read_Clicked();
    void Port_Dlbox_Read_Data();
    QByteArray Pc_To_Dlbox_Compose(uint8_t CmdStu, QByteArray CmdData);
    bool DlboxSerial_LinkCmd_Link();
    // Timer
    void Port_Dlbox_Once_Timer();
    void Port_Dlbox_Sync_Timer();
    // Mtsn
    void Dlbox_MtsnCode_Write(bool DevStu);
    bool DlboxSerial_LinkCmd_ReadMtsn();
    bool DlboxSerial_LinkCmd_BurnMtsn(QByteArray CmdData);
    bool DlboxSerial_LinkCmd_ReadData();
    bool DlboxSerial_LinkCmd_BurnData(QByteArray CmdData);

// url
    void Url_Init();
    void OpenUrl(QString url);

// CFG
    void Gui_Init_CFG();
    void TextBox_CFG_Langure_Change_Init();
    void tw_cont_cfg_checkbox_add(QString NameKey, int row, int column);
    void tw_cont_cfg_checkbox_inline_statechanged(QSqlTableModel* SqlMode_Cfg, int RowNum, bool CheckStu);
    void tw_title_cfg_CheckState();
// CFG-SQL
    void Cfg_Sql_Init();
// BAT-LOOP
    void Port_f316_Port_Init();
    void Port_f316_Sync_Timer();
    void Port_f316_Once_Timer();
    void Port_f316_Send_Data(QByteArray LineStr,int SendStu);
    void Port_f316_Send_Repeat();
    void Port_f316_Read_clicked();
    void Port_f316_Read_Data();
    void Port_f316_Recv_Data_Answer(int WaitStu);
    void Port_f316_Recv_Data_WaitOut(int TimeVal);
    bool Port_f316_Send_Data_Times(QByteArray SendBuff, int LastStu, int SendStu);
    bool Port_f316_Upgrade_File();
    bool Port_f316_Read_Param();
    bool Port_f316_Write_Param();
    void on_btn_unlink_f316_clicked();
    void Port_F316_Lock_Or_Unlock(int EnabledStu);
    void Port_F310_Get_File_Vers();
    void Lbl_Ver_F316_Langure_to_Change();
	
private slots:
    void on_btn_link_clicked();
    void on_btn_file_write_clicked();

    //顶部按钮组
    void on_btn_menu_close_clicked();
    void on_btn_menu_max_clicked();
    void on_btn_menu_min_clicked();
    void on_btn_menu_menu_clicked();
    void ActHelp_to_Disp();
    void Menu_Option_Drive341A_clicked();
    void Menu_Option_Drive341Ser_clicked();
    void Menu_Option_DrivePL23_clicked();
    void Menu_Option_DrivePL23W11_clicked();
    void Menu_Option_Drive343Ser_clicked();
    void Menu_Option_Force_Upgrade_File_clicked();
    void Menu_Option_AboutUs_clicked();
    void Menu_Option_TPTools_3uTools_Clicked();
    void Menu_Option_TPTools_I4_Clicked();
    void Menu_Option_TPTools_ShaLou_Clicked();

    //void on_btn_main_clicked();
    void on_btn_menu_drive_clicked();
    void on_btn_menu_langure_clicked();

    //原彩修复按钮组
    void on_cbx_upg_clicked();
    void on_btn_upg_clicked();
    void on_btn_mtsn_read_clicked();
    void on_cbx_mtsn_write_clicked();
    void on_btn_mtsn_write_clicked();
    void on_btn_read_clicked();
    void on_cbx_write_clicked();
    void on_btn_write_clicked();

    //dl8000-按钮
    void on_btn_link_dl8000_clicked();
    void on_btn_mtsn_read_dl8000_clicked();
    void on_btn_mtsn_write_dl8000_clicked();
    void on_cbx_model_read_dl8000_activated(int index);
    void on_cbx_mtsn_write_dl8000_clicked();
    void on_btn_upg_dl8000_clicked();

    //右键菜单
    void on_te_prin_customContextMenuRequested(const QPoint &pos);
    void on_te_file_write_customContextMenuRequested(const QPoint &pos);
    void on_te_mtsn_customContextMenuRequested(const QPoint &pos);
    void on_te_prin_dlbox_customContextMenuRequested(const QPoint &pos);
    void on_te_mtsn_dlbox_customContextMenuRequested(const QPoint &pos);
    void on_te_file_write_dlbox_customContextMenuRequested(const QPoint &pos);
    void on_te_prin_disp_f210_customContextMenuRequested(const QPoint &pos);
    void on_btn_mtsn_write_customContextMenuRequested(const QPoint &pos);
    void on_btn_mtsn_write_dlbox_customContextMenuRequested(const QPoint &pos);
    void on_te_prin_bat_customContextMenuRequested(const QPoint &pos);
    void on_te_prin_f316_customContextMenuRequested(const QPoint &pos);
    void on_te_mtsn_dl8000_customContextMenuRequested(const QPoint &pos);
    void Menu_Option_Clear();
    void Menu_Option_Paste();
    void Menu_Option_Copy();
    void Menu_Option_SelectAll();
    void Menu_Option_Import_Upgrade_File();

    //侧方栏按钮组
    void on_btn_left_mtsn_f210_clicked();
    void on_btn_left_mtsn_dlbox_clicked();
	void on_btn_left_dl8000_clicked();

    void Wait_TimeOut_to_Mcu_Answer(int WaitStu);

    void on_cbx_model_read_activated(int index);

    void on_btn_ContactUs_clicked();

    bool Check_cbx_model_read_Must_Select(QComboBox* cbx_model_read, QTextEdit *te_prin);

    void on_te_mtsn_textChanged();
    void on_te_mtsn_dlbox_textChanged();
    void on_te_mtsn_dl8000_textChanged();

    void btn_choose_import_filepath_clicked();
    void btn_import_file_clicked();

    void on_cbx_model_read_dlbox_activated(int index);

    void on_btn_link_dlbox_clicked();

    void Dlbox_Btn_Lock_Or_Unlock(int EnabledStu);

    void on_cbx_mtsn_write_dlbox_clicked();

    void on_cbx_write_dlbox_clicked();

    void on_btn_mtsn_read_dlbox_clicked();

    void on_btn_mtsn_write_dlbox_clicked();

    void on_btn_read_dlbox_clicked();

    void on_btn_write_dlbox_clicked();

    void on_btn_file_write_dlbox_clicked();

    void on_btn_left_disp_f210_clicked();

    bool Read_Disp_Data_Form_Mcu(QString FilePath);
    bool Write_Disp_Data_Form_Mcu(QString WriteFileName);

    void on_btn_link_disp_f210_clicked();
    void disp_f210_data_receive(QByteArray data);

    void on_btn_disp_read_f210_clicked();

    void on_btn_disp_write_f210_clicked();

    void on_cbx_disp_write_f210_clicked();

    void DevData_to_Btn_Mtsn_Write_RightClicked();
    void DevData_to_Btn_Mtsn_Write_Dlbox_RightClicked();

    bool Wait_TimeOut_to_Wait_Dlbox_Answer();

    void on_btn_left_bat_f210_clicked();

    void on_btn_link_bat_clicked();

    void on_btn_read_data_bat_clicked();

    void on_btn_write_data_bat_clicked();

    // dlbox_CFG
    void on_btn_left_cfg_dlbox_clicked();
    void on_btn_file_read_cfg_clicked();
    void on_te_file_read_cfg_textChanged();
    void tw_title_cfg_checkbox_statechanged();
    void tw_cont_cfg_checkbox_statechanged();
    // dlbox_SQL
    void on_btn_left_sql_dlbox_clicked();
    void on_btn_sel_sql_clicked();
    void on_btn_add_sql_clicked();
    void on_btn_del_sql_clicked();
    void on_btn_upd_sql_clicked();
    void on_btn_ret_sql_clicked();

    void on_le_SN_textChanged(const QString &arg1);

    void on_sb_BatHeath_valueChanged(int arg1);

    void on_btn_left_loop_f316_clicked();

    void on_btn_link_f316_clicked();

    void on_btn_upg_f316_clicked();

    void on_btn_read_f316_clicked();

    void on_btn_write_f316_clicked();

    void on_btn_default_f316_clicked();

    void on_cbx_upg_f316_clicked();

    void on_hs_cc_f316_valueChanged(int value);
    void on_ds_cc_f316_valueChanged(double arg1);
    void on_hs_dc_f316_valueChanged(int value);
    void on_ds_dc_f316_valueChanged(double arg1);
    void on_hs_cctime_f316_valueChanged(int value);
    void on_sb_cctime_f316_valueChanged(int arg1);
    void on_hs_dctime_f316_valueChanged(int value);
    void on_sb_dctime_f316_valueChanged(int arg1);

    void on_cbx_upg_dl8000_clicked();

protected:
    // 鼠标移动事件函数
    void mouseMoveEvent(QMouseEvent *event);
    // 鼠标按键按下事件函数
    void mousePressEvent(QMouseEvent *event);
    // 鼠标按键抬起事件函数
    void mouseReleaseEvent(QMouseEvent *event);
    bool nativeEvent(const QByteArray &eventType, void *message, long *result);

    bool eventFilter(QObject *watched, QEvent *event);

private:
    QPoint m_ptMouseLatestPos;      // 鼠标的最后位置
    bool m_bMouseLftBtnPressed;     // 标识当前是否按下鼠标左键

    int boundaryWidth;
    QPoint clickPos;

    int MaxStu;

private://
    Ui::MainWindow *ui;

    QList<int> iconsMain;
    QList<int> IconsLeft;
    QList<QAbstractButton *> MainBtns;
    QList<QAbstractButton *> LeftBtns;
    QList<QAbstractButton *> LeftBtns_f210;
    QList<QAbstractButton *> LeftBtns_dlbox;
    QList<QAbstractButton *> LeftBtns_f316;
    QList<QAbstractButton *> LeftBtns_dl8000;

    QIcon *MaxIcon;

    QString AppTitle;
    QString LangureStr;

    int LangureStu;

    QSettings *CfgFile;
    QSettings *CfgFile_Dlbox;

private:
    //根据QSS样式获取对应颜色值
    QString borderColor;
    QString normalBgColor;
    QString darkBgColor;
    QString normalTextColor;
    QString darkTextColor;

    void getQssColor(const QString &qss, const QString &flag, QString &color);
    void getQssColor(const QString &qss, QString &textColor,
                     QString &panelColor, QString &borderColor,
                     QString &normalColorStart, QString &normalColorEnd,
                     QString &darkColorStart, QString &darkColorEnd,
                     QString &highColor);
private://端口属性
    QSerialPort *PortSerial;
    QStringList portStringList;

    int PageCnt;
    int RecvCnt;
    int RecvSize;
    int AllFileSize;
    bool RecvStu;
    int ModelStu;
    int ConnectStu;
    int PortStu;
    int LastPortStu;
    int PortVer;
    int UpgStu; //0.无升级 1.仅升级MCU 2.升级MCU和FPGA

    QByteArray SendData;
    QByteArray RecvData;
    QByteArray RecvMtSN;
    QByteArray RecvStr;
    QByteArray BinVersion;
    QByteArray DevVersion;
    QByteArray BinVerDate;
    QByteArray DevVerDate;
    QByteArray DefMtSN;

    QString FilePath;
    QString ReadFileName;
    QString BurnFileName;
    QString VersFileName;
    QString FpgaFileName;
    QString VersF3xName;
    QString DevsF3xName;
    QString VersData;

    QThread *SerialThread;
    QThread StuThread;
// F210-电池修复
    QByteArray BatData;
private://导航栏子菜单
    bool HoverStu_f210;
    QIcon* DownIcon;
    QIcon* RightIcon;
    QIcon* DownIcon_Hover;
    QIcon* RightIcon_Hover;
private://定时器
    int TimeOutCnt;
    int WaitOutCnt;
    int SendOutCnt;

    QTimer* timer;
    QTimer* RecvTimer;
    int RecvTimerId;
    QTimer* SyncTimer;
    int SyncTimerId;
    QTimer *WaitTimer;

private://个性化控件
    QHexMonitor *hexEdit;
    QHexMonitor *hexEdit_code_f210;

    QFont AppFont;

    contactus *CallWdg;
private://右键菜单
    //
    QMenu *RightMenu_Write;
    QMenu *Right_Write_dlbox;
    QAction *ActDevData;
    QAction *ActDevData_dlbox;

    //
    QMenu *RightMenu;
    QAction *ActSelAll;
    QAction *ActCopy;
    QAction *ActPaste;
    QAction *ActClear;

    int MenuStu;    //控件类型
//菜单按钮
    QMenu *BtnMenuMenu;
    QAction *ActHelp;
    //驱动
    QMenu *MenuDrive;
    QAction *ActD341A;
    QAction *ActD341Ser;
    QAction *ActPL23;
    QAction *ActPL23W11;
    QAction *ActD343Ser;
    //F210强制升级
    QAction *AutoUpgFile;
    //
    QAction *ImportFile;
    //关于
    QAction *AboutUs;
    //第三方工具
    QAction *ActTPTools;
    QMenu *MenuTPTools;
    QAction *Act3uTools;
    QAction *ActI4;
    QAction *ActShaLou;

    QTextEdit* te_importfile;
    QLabel* lbl_importstu;
private: //显示修复线程
    comthread_f210 *ComThread_disp_f210;
    QByteArray RespData_disp_f210;
private: //串口
    int ConnectStu_disp_f210;
    int ConnectStu_dlbox;
    QStringList PortList_f210;
    QStringList PortList_disp_f210;
    QStringList PortList_dlbox;
    QString CPortName;
    QString CPortName_dlbox;
    QString CPortName_disp_f210;
private: //dlbox
    bool HoverStu_dlbox;
    QHexMonitor *hexEdit_dlbox;
	
    QSerialPort *Port_dlbox;
    QThread *Thread_dlbox;
	
    QTimer* RecvTimer_dlbox;
    QTimer* SyncTimer_dlbox;
    QTimer* WaitTimer_dlbox;

    int PortStu_dlbox;
    int RespStu_dlbox;
    int RespTime_dlbox;
    int RespWait_dlbox;
    int OutCnt_dlbox;
    int ModelStu_dlbox;
    int DataCnt_dlbox;
	
    QByteArray RecvStr_dlbox;
    QByteArray RespErrMsg_dlbox;
    QByteArray RespMtsn_dlbox;
    QByteArray RespData_dlbox;

    QString RespVers_dlbox;
    QString RespSN_dlbox;

    uint16_t SendAddr_dlbox;  //Mtsn Addr
private: //dlbox_cfg
    QSqlDatabase SqlData_Cfg;
    QStringList  TypeList_Cfg;
    int CheckSum_Cfg;//总计数
    int CheckCnt_Cfg;//选中数
private: //dlbox_sql
    QSqlTableModel *SqlMode;
private: //f316
    bool HoverStu_f316;
    QSerialPort *Port_f316;
    QStringList PortList_f316;
    QThread *Thread_f316;
    QString CPortName_f316;
	
    int FileSize_f316;
    int SendCnt_f316;
    int PageCnt_f316;
    int ConnStu_f316;
    int PortStu_f316;
    int LastPortStu_f316;
    bool RecvStu_f316;
    int TimeOutCnt_f316;
    int WaitOutCnt_f316;

    QTimer* RecvTimer_f316;
    QTimer* SyncTimer_f316;
    QTimer *WaitTimer_f316;
	
    QByteArray SendData_f316;
    QByteArray RecvData_f316;
    QByteArray BinVer_f316;
    QByteArray DevVer_f316;

    int hs_cc_f316_flag = 0;
    int hs_dc_f316_flag = 0;
    int hs_cctime_f316_flag = 0;
    int hs_dctime_f316_flag = 0;
private: //dl8000
    bool HoverStu_dl8000;
	
    QSerialPort *Port_dl8000;
    QStringList PortList_dl8000;
    QThread *Thread_dl8000;
    QString CPortName_dl8000;
    int RecvCnt_dl8000;
    int RecvSize_dl8000;
    int AllFileSize_dl8000;

    int ModelStu_dl8000;
    int PageCnt_dl8000;
    int ConnStu_dl8000;
    int PortStu_dl8000;
    int LastPortStu_dl8000;
    bool RecvStu_dl8000;
    int TimeOutCnt_dl8000;
    int WaitOutCnt_dl8000;
    int SendOutCnt_dl8000;

    QTimer* RecvTimer_dl8000;
    QTimer* SyncTimer_dl8000;
    QTimer *WaitTimer_dl8000;

    QByteArray SendData_dl8000;
    QByteArray RecvData_dl8000;
    QByteArray RecvMtSN_dl8000;
    QByteArray RecvStr_dl8000;
    QByteArray BinVer_dl8000;
    QByteArray DevVer_dl8000;
    QString VersName_dl8000;
    QString DevsName_dl8000;
};
#endif // MAINWINDOW_H
